/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javalee.dao;

import javalee.maps.Maquina;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;
/**
 *
 * @author Danilo
 */
public class maquinaDAO extends DAO{
     private Statement stmt = null;

    @Override
public boolean cadastrar(Object obj){
        
        try{
            Maquina m = (Maquina) obj;
            String SQL = "insert into maquina (descricao) values ('"+m.getDescricao()+"')";
            stmt = conexaoBanco.createStatement();
            stmt.executeUpdate(SQL);
            stmt.close();
            //"insert into cliente (nome) values ('?')"
            return (true);
        } 
           catch (SQLException ex) {
             System.out.println("Problema com o SQL");
            // ex.printStackTrace();
             return (false);
       }
        
    }
    @Override
public boolean alterar(Object obj){
         try{
            Maquina m = (Maquina) obj;
            String SQL = "update maquina set descricao='"+m.getDescricao()+"' where id = "+m.getId();
            stmt = conexaoBanco.createStatement();
            stmt.executeUpdate(SQL);
            stmt.close();
            //"insert into cliente (nome) values ('?')"
            return (true);
        } 
           catch (SQLException ex) {
             System.out.println("Problema com o SQL");
           //  ex.printStackTrace();
             return (false);
       }
        }
    @Override
public boolean excluir(Object obj){
    try{
            Maquina m = (Maquina) obj;
            String SQL = "delete from maquina where id = "+m.getId();
            stmt = conexaoBanco.createStatement();
            stmt.executeUpdate(SQL);
            stmt.close();
            //"insert into cliente (nome) values ('?')"
            return (true);
        } 
           catch (SQLException ex) {
             System.out.println("Problema com o SQL");
             System.out.println(ex.getErrorCode());
             
             if(ex.getErrorCode()==1451)
             {
                 JOptionPane.showMessageDialog(null,"Existe um produto usando esta máquina.\nMáquina não pode ser excluída.");
             }
            // ex.printStackTrace();
             return (false);
       }}
    @Override
public ArrayList consultar(Object obj){
        
        ArrayList<Maquina> lista = new ArrayList<Maquina>();
        //List<Person> personlist = new List<Person>();
        try {
            stmt = conexaoBanco.createStatement();
            String query = "SELECT id, descricao from maquina";
            if(!("".equals(obj.toString()))){
                query = query.concat(" WHERE descricao LIKE '"+obj.toString()+"%'");
            }
            query += " LIMIT 100";
            ResultSet srs = stmt.executeQuery(query);
            while (srs.next()) {
                Maquina m = new Maquina();
                m.setDescricao(srs.getString("descricao"));
                m.setId(srs.getInt("id"));
                
                lista.add(m);
            }
           
            stmt.close();
        }
        
           catch (SQLException ex) {
             System.out.println("Problema com o SQL");
             //ex.printStackTrace();
             return (null);
       }
 return lista;
}
}
